﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuckyLoot.ServiceFacade.Contracts;
using LuckyLoot.Domain.Entities.People;
using LuckyLoot.Domain.Entities;
using LuckyLoot.Common.Repositories;
using LuckyLoot.Domain.Aggregate;
using LuckyLoot.Common.Contexts;

namespace LuckyLoot.ServiceFacade.Implementation
{
    public class ReportService : IReportService
    {
        #region IReportService Members

        public Dictionary<Customer, int> GetLoyalCustomers()
        {
            var loyalCustomers = new Dictionary<Customer, int>();
            //SELECT COUNT(*) AS TattooCount, Customer_Id FROM Apointments GROUP BY Customer_Id ORDER BY TattooCount DESC

            using (ApplicationContext repository = new ApplicationContext())
            { 
                var query = from tattoo in repository.Apointments
                            where tattoo is TattooApointment
                            group tattoo by tattoo.Customer into customers
                        select new { Customer = customers.Key, TattooCount = customers.Count() };

                var list = query.ToList().OrderBy(a => a.TattooCount);

                foreach (var rec in list)
                {
                    loyalCustomers.Add(rec.Customer, rec.TattooCount);
                }
            }

            return loyalCustomers;
        }

        public List<Supply> GetMostUsedSupplies()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
